<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class FeedAction extends HomeAction
{

	private $Feed;
    private $siteUrl;
    private $pageSize;
    private $fulltextEnable;


	public function _initialize()
	{
		parent::_initialize();
		
		import('ORG.Util.Input');
		vendor('FeedWriter.FeedWriter');
		
		$this->Feed = new FeedWriter(RSS2);
		$this->siteDomain = get_site_domain();
		$this->pageSize = !empty($this->_global['max_feed_items']) ? $this->_global['max_feed_items'] : 50;
		$this->fulltextEnable = $this->_global['feed_fulltext_enable'];
	}


	//分类Rss
	public function category()
    {
    	$cateID = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
    	$map = $order = array();
    	if (!empty($cateID))
    	{
    	    $categories = F('cache_categories');
    		$cateIDs  = get_sub_category_ids($cateID, 1);
    		$category = $categories[$cateID];
    		$map['category_id'] = array('in', $cateIDs);
    	}
    	$order['question_id'] = 'desc';

		$Question = D('Question');
    	$questions = $Question->where($map)->order($order)->limit($this->pageSize)->select();

    	if (!empty($questions))
    	{
    		$title = !empty($category) ? $category['name'] : L('all_categories');
    		$description = !empty($category['description']) ? $category['description'] : L('rss_latest_questions');
    		$this->Feed->setTitle($title . ' - ' . $this->_global['site_name']);
    		$this->Feed->setLink(format_category_url($cateID));
    		$this->Feed->setDescription($description);
    		$this->Feed->setImage($this->_global['site_name'], $this->_global['site_logo'], $this->siteDomain);
    		$this->Feed->setChannelElement('copyright', $this->_global['site_name']);
    		$this->Feed->setChannelElement('lastBuildDate', format_date());

    		foreach ($questions as $key => $val)
    		{
    			$newItem = $this->Feed->createNewItem();
    			$link = format_question_url($val['question_id'], $val['title']);
    			$newItem->setTitle($val['title']);
    			$newItem->setLink($link);
    			$newItem->setDate(format_date($val['create_time']));
    			if($this->fulltextEnable)
    			{
    				$newItem->setDescription(parse_markup($val['details']));
    			}
    			$newItem->addElement('category', $category['name']);
    			$newItem->addElement('author', $val['poster_name']);
    			$this->Feed->addItem($newItem);
    		}
    		echo $this->Feed->generateFeed();
    	}
    }


    //单个问题Rss
    public function question()
    {
    	$Question = D('Question');
    	
    	$qstID = intval($_GET['qid']);

    	$question = $Question->find($qstID);
    	if (!$question)
    	{
    		$this->error(L('question_not_existed'));
    	}

		$Answer = D('Answer');
		$map   = array('question_id' => $qstID);
		$order = array('answer_id' => 'desc');
    	$answers = $Answer->where($map)->order($order)->limit($this->pageSize)->select();

		$this->Feed->setTitle($question['title'] . ' - ' . $this->_global['site_name']);
		$this->Feed->setLink(format_question_url($question['question_id'], $question['title']));
		$this->Feed->setDescription(parse_markup($question['details']));
		$this->Feed->setImage($this->_global['site_name'], $this->_global['site_logo'], $this->siteDomain);
		$this->Feed->setChannelElement('copyright', $this->_global['site_name']);
		$this->Feed->setChannelElement('lastBuildDate', format_date($question['create_time']));

		if (!empty($answers))
		{
        	foreach ($answers as $val)
        	{
        	    $answeredBy = sprintf(L('rss_answered_by'), $val['poster_name']);
				$newItem = $this->Feed->createNewItem();
				$newItem->setTitle($answeredBy);
				$newItem->setLink(format_question_url($val['question_id'], $val['title']));
				$newItem->setDate(format_date($val['create_time']));
				if ($this->fulltextEnable)
				{
					$newItem->setDescription(parse_markup($val['details']));	
				}
				$newItem->addElement('author', $val['poster_name']);
				$this->Feed->addItem($newItem);
			}
			echo $this->Feed->generateFeed();
		}
    }


    //用户已回答问题Rss
    public function user()
    {
    	$userID = intval($_GET['uid']);
    	$user   = $this->_User->find($userID);
    	$perms  = $this->_User->getShareSettings($this->_user['user_id'], $userID);

    	if (empty($user))
    	{
    		$this->error(L('user_not_existed'));
    	}
    	
    	if (empty($perms['share_qa_activity']) && $this->_user['user_id'] != $userID)
    	{
            $this->error(L('user_private_activity'));
       	}

		$View  = D('QuestionAnswerView');
		$map   = array('Answer.poster_id' => $userID);
		$order = array('Question.question_id' => 'desc');
		$questions = $View->where($map)->order($order)->limit($this->pageSize)->select();

		if (!empty($questions))
		{
			$Question = D('Question');
			
			$title = $description = sprintf(L('rss_question_answered_by'), $user['username']);
    		$this->Feed->setTitle($title . ' - ' . $this->_global['site_name']);
    		$this->Feed->setLink(format_user_url($user['user_id'], $user['username']));
    		$this->Feed->setDescription($description);
    		$this->Feed->setImage($this->_global['site_name'], $this->_global['site_logo'], $this->siteDomain);
    		$this->Feed->setChannelElement('copyright', $this->_global['site_name']);
    		$this->Feed->setChannelElement('lastBuildDate', format_date());

    		foreach ($questions as $key => $val)
    		{
    			$newItem = $this->Feed->createNewItem();
    			$newItem->setTitle($val['title']);
    			$newItem->setLink(format_question_url($val['question_id'], $val['title']));
    			$newItem->setDate(format_date($val['create_time']));
    			if ($this->fulltextEnable)
    			{
    				$map = array('question_id' => $val['question_id']);
    				$details = $Question->where($map)->getField('details');
    				$newItem->setDescription(parse_markup($details));	
    			}
    			$newItem->addElement('author', $val['username']);
    			$this->Feed->addItem($newItem);
    		}
    		echo $this->Feed->generateFeed();
		}
    }


    //标签Rss
    public function tag()
    {
    	$tagID = intval($_GET['tid']);
    	$Tag   = D('Tag');
    	$tag   = $Tag->find($tagID);
    	$View  = D('QuestionTagUserView');
    	
    	$map   = array('tag_id' => $tagID);
    	$order = array('Question.question_id' => 'desc');
    	$questions = $View->where($map)->order($order)->limit($this->pageSize)->select();

    	if ($questions) 
    	{	
    		$Question = D('Question');
    		
    		$title = sprintf(L('tagged_questions'), $tag['name']);
    		$this->Feed->setTitle($title . ' - ' . $this->_global['site_name']);
    		$this->Feed->setLink($this->siteDomain . format_tag_url($tag['tag_id'], $tag['name']));
    		$this->Feed->setDescription($title);
    		$this->Feed->setImage($this->_global['site_name'], $this->_global['site_logo'], $this->siteDomain);
    		$this->Feed->setChannelElement('copyright', $this->_global['site_name']);
    		$this->Feed->setChannelElement('lastBuildDate', format_date());

    		foreach ($questions as $key => $val) {
    			$newItem = $this->Feed->createNewItem();
    			$link = $this->siteDomain . format_question_url($val['question_id'], $val['title']);
    			$newItem->setTitle($val['title']);
    			$newItem->setLink($link);
    			$newItem->setDate(format_date($val['create_time']));
    			if ($this->fulltextEnable)
    			{
    				$map = array('question_id' => $val['question_id']);
    				$details = $Question->where($map)->getField('details');
    				$newItem->setDescription(parse_markup($details));	
    			}
    			$newItem->addElement('category', get_category_name($val['category_id']));
    			$newItem->addElement('author', $val['username']);
    			$this->Feed->addItem($newItem);
    		}
    		echo $this->Feed->generateFeed();
    	}
    }
    
}


/* End of file FeedsAction.class.php */
/* Location: ./App/Lib/Action/Home/FeedAction.class.php */
